{extend name="public:form" /}
{block name="menu"}角色管理 / 权限分配{/block}
{block name="css"}
<link rel="stylesheet" href="__PLUGINS__/css/ztree/bootstrapStyle/bootstrapStyle.css" type="text/css">
{/block}
{block name="form"}
<ul id="tree" class="ztree text-center"></ul>
{/block}
{block name="js"}
<script type="text/javascript" src="__PLUGINS__/js/ztree/jquery.ztree.core.js"></script>
<script type="text/javascript" src="__PLUGINS__/js/ztree/jquery.ztree.excheck.js"></script>
<script type="text/javascript" src="__PLUGINS__/js/ztree/jquery.ztree.exedit.js"></script>
<script>
    let setting = {
        view: {},
        check: {enable: true},
        async : {
            enable : true,
            url : '{:url("role/getPermissionsOfRole")}',
            otherParam : {"role_id" : "{$role_id}"},
            type: "post"
        },
        data: {simpleData: {enable: true, pIdKey : "pid",}},
        callback:{
            onAsyncSuccess: zTreeOnAsyncSuccess,
            onCheck:onCheck
        }
    };
    let ids;
    $(document).ready(function(){
        $.fn.zTree.init($("#tree"), setting, null);
    });
    function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
        ids = [];
        let treeObj=$.fn.zTree.getZTreeObj("tree");
        nodes = treeObj.getCheckedNodes(true);
        for(let i=0; i<nodes.length; i++){
            ids.push(nodes[i].id); //获取选中节点的值
        }
        console.log(ids)
    };
    function onCheck(e,treeId,treeNode){
        ids = [];
        let treeObj=$.fn.zTree.getZTreeObj("tree");
        nodes = treeObj.getCheckedNodes(true);
        for(let i=0; i<nodes.length; i++){
            ids.push(nodes[i].id); //获取选中节点的值
        }
        console.log(ids)
    }
    $(".btn-primary").click(function(){
        $.post("{:url('role/givePermissions')}", {role_id:"{$role_id}", permissions: ids}, function(response){
            if (!response.code ) {
                warning(response.msg); return false;
            }
            success(response.msg)
            setTimeout(function(){
                window.location.href = response.url
            }, response.wait * 1000);
        });
    })
</script>
{/block}
